package com.livestocksolutions.ui;

import com.livestocksolutions.R;
import com.livestocksolutions.intent.BaseActivity;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

/* -- Version History --
 *  1.0 : 03 August 2012 - Initial version.
 *  1.1 : 03 September 2012 - added setFocused() method and changed context from type Context to child
 *                          class BaseActivity.
 */

/**
 * A class to create the layout for the list of Contacts
 * @author Adam Parr
 * @version 1.1
 * 
 * @.modified 16 September 2012
 */
public class ViewOwnerArrayAdapter extends BaseArrayAdapter {
	/**
	 * Size of the allowable array passed
	 */
	public static final int SIZE = 7;
	/**
	 * Id of this Owner
	 */
	public static final int ID = 0;
	/**
	 * Name of the Owner position within the String[position][{@value #NAME}] array passed in the Constructor.
	 */
	public static final int NAME = 1;
	/**
	 * Type of the Owner position within the String[position][{@value #TYPE}] array passed in the Constructor.
	 */
	public static final int TYPE = 2;
	/**
	 * For a type Farm or Business Owner String[position][{@value #STREETADDRESS}] will be the Street address value,
	 * otherwise this will be an empty string
	 */
	public static final int STREETADDRESS 	= 3;
	/**
	 * For a type Farm or Business Owner String[position][{@value #TOWNADDRESS}] will be the Town value,
	 * otherwise this will be an empty string
	 */
	public static final int TOWNADDRESS 		= 4;
	/**
	 * For a type Farm or Business Owner String[position][{@value #POSTCODEADDRESS}] will be the Postcode value,
	 * otherwise this will be an empty string
	 */
	public static final int POSTCODEADDRESS 	= 5;
	/**
	 * For a type Farm Owner String[position][{@value #FARMPIC}] will be the PIC value,
	 * otherwise this will be an empty string
	 */
	public static final int FARMPIC 			= 6;
	
	public static final int TYPE_BUSINESS 	= 0;
	public static final int TYPE_FARM 		= 1;
	public static final int TYPE_OTHER 		= 2;

	/**
	 * Constructor
	 * @param context The current context.
	 * @param values The Contacts to represent in the ListView.
	 */
	public ViewOwnerArrayAdapter(BaseActivity context, String[] values) {
		super(context, new String[][]{values});
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		setView(R.layout.item_owner);
		final View rowView = super.getView(position, convertView, parent);
		
		rowView.findViewById(R.id.contactAddressInclude).findViewById(R.id.contactType).setVisibility(View.GONE);
		
		EditText ownerName = (EditText) rowView.findViewById(R.id.ownerName);
		Spinner ownerType = (Spinner) rowView.findViewById(R.id.ownerType);

		ownerName.setText(values[position][NAME]);
		
		TextView farmPIC = (TextView) rowView.findViewById(R.id.farmPIC);
		TextView contactField = (TextView) rowView.findViewById(R.id.contactField);
		TextView addressTown = (TextView) rowView.findViewById(R.id.addressTown);
		TextView addressPostcode = (TextView) rowView.findViewById(R.id.addressPostal);
		
		// Create this array to be the full length that will be needed to avoid ArrayIndexOutOfBoundsException
		if(values[position].length<SIZE){
			String[] orig = values[position];
			values[position] = new String[SIZE];
			int x = 0;
			for(String s : orig){
				values[position][x]=s;
				x++;
			}
		}
		
		// Set the current values of the Owner address and type object
		farmPIC.setText(values[position][FARMPIC]);
		contactField.setText(values[position][STREETADDRESS]);
		addressTown.setText(values[position][TOWNADDRESS]);
		addressPostcode.setText(values[position][POSTCODEADDRESS]);

		ownerName.addTextChangedListener(new TextWatcher() {
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
			public void onTextChanged(CharSequence s, int start, int before, int count) {}

			public void afterTextChanged(Editable s) {
				values[position][NAME]=s.toString();
			}
		});
		
		ownerType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view, int index, long id) {
				View farmPICSpace = rowView.findViewById(R.id.farmPICSpace);
				View addressSpace = rowView.findViewById(R.id.addressSpace);
				
				values[position][TYPE]=String.valueOf(index);
			
				// Set what is visible and not visible
				switch(index){
					case TYPE_BUSINESS:
						// array[0] (Business)	
						farmPICSpace.setVisibility(View.GONE);
						addressSpace.setVisibility(View.VISIBLE);
						break;
					case TYPE_FARM:
						// array[1] (Farm)
						farmPICSpace.setVisibility(View.VISIBLE);
						addressSpace.setVisibility(View.VISIBLE);
						break;
					case TYPE_OTHER:
						// array[2] (Other)
						farmPICSpace.setVisibility(View.GONE);
						addressSpace.setVisibility(View.GONE);
						break;
					default:
				}
			}

			public void onNothingSelected(AdapterView<?> parent) {}
		});
		
		farmPIC.addTextChangedListener(new TextWatcher() {
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
			public void onTextChanged(CharSequence s, int start, int before, int count) {}

			public void afterTextChanged(Editable s) {
				//TODO add validation
				values[position][FARMPIC]=s.toString();
			}
		});

		contactField.addTextChangedListener(new TextWatcher() {
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
			public void onTextChanged(CharSequence s, int start, int before, int count) {}

			public void afterTextChanged(Editable s) {
				// TODO possible validation technique
				values[position][STREETADDRESS]=s.toString();
			}
		});

		addressTown.addTextChangedListener(new TextWatcher() {
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
			public void onTextChanged(CharSequence s, int start, int before, int count) {}

			public void afterTextChanged(Editable s) {
				// TODO possible validation technique
				values[position][TOWNADDRESS]=s.toString();
			}
		});
		
		addressPostcode.addTextChangedListener(new TextWatcher() {
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
			public void onTextChanged(CharSequence s, int start, int before, int count) {}

			public void afterTextChanged(Editable s) {
				// TODO validation
				values[position][POSTCODEADDRESS]=s.toString();
			}
		});
		
		// Determine the Owner type this owner is
		ownerType.setSelection(Integer.parseInt(values[position][TYPE]!=null?values[position][TYPE]:"0"));
		
		return rowView;
	}

	/**
	 * Get a View that displays the data in the data set. You can either create a View manually
     * or inflate it from an XML layout file. When the View is inflated, the parent View (GridView,
     * ListView...) will apply default layout parameters unless you use
     * {@link android.view.LayoutInflater#inflate(int, android.view.ViewGroup, boolean)}
     * to specify a root view and to prevent attachment to the root.
     * 
     * @param convertView The old view to reuse, if possible. Note: You should check that this view
     *        is non-null and of an appropriate type before using. If it is not possible to convert
     *        this view to display the correct data, this method can create a new view.
     *        Heterogeneous lists can specify their number of view types, so that this View is
     *        always of the right type (see {@link android.widget.BaseAdapter#getViewTypeCount()} and
     *        {@link android.widget.BaseAdapter#getItemViewType(int)}).
     * @param parent The parent that this view will eventually be attached to
     * @return A View corresponding to the data at the specified position.
	 */
	public View getView(View convertView, ViewGroup parent) {
		return super.getView(0, convertView, parent);
	}
}
